home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / wrlib / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-03-09  |  5.9 KB  |  226 lines

  1. /* 
  2.  *  Raster graphics library
  3.  * 
  4.  *  Copyright (c) 1997-2000 Alfredo K. Kojima
  5.  *
  6.  *  This library is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU Library General Public
  8.  *  License as published by the Free Software Foundation; either
  9.  *  version 2 of the License, or (at your option) any later version.
  10.  *  
  11.  *  This library is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  *  Library General Public License for more details.
  15.  *  
  16.  *  You should have received a copy of the GNU Library General Public
  17.  *  License along with this library; if not, write to the Free
  18.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20. #include <config.h>
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <X11/Xlib.h>
  26.  
  27. #include "wraster.h"
  28.  
  29.  
  30. void 
  31. RBevelImage(RImage *image, int bevel_type)
  32. {
  33.     RColor color;
  34.     RColor cdelta;
  35.     int w, h;
  36.  
  37.     if (image->width<3 || image->height<3)
  38.         return;
  39.  
  40.     w = image->width;
  41.     h = image->height;
  42.     if (bevel_type>0) {  /* raised */
  43.         /* top */
  44.         cdelta.alpha = 0;
  45.         cdelta.red = cdelta.green = cdelta.blue = 80;
  46.         ROperateLine(image, RAddOperation, 0, 0, w-1, 0, &cdelta);
  47.         if (bevel_type==RBEV_RAISED3 && w>3)
  48.             ROperateLine(image, RAddOperation, 1, 1, w-3, 1,&cdelta);
  49.  
  50.         /* left */
  51.         ROperateLine(image, RAddOperation, 0, 1, 0, h-1, &cdelta);
  52.         if (bevel_type==RBEV_RAISED3 && h>3)
  53.             ROperateLine(image, RAddOperation, 1, 2, 1, h-3, &cdelta);
  54.  
  55.     /* bottom */
  56.         color.alpha = 255;
  57.         color.red = color.green = color.blue = 0;
  58.         cdelta.red = cdelta.green = cdelta.blue = 40;
  59.         if (bevel_type==RBEV_RAISED2 || bevel_type==RBEV_RAISED3) {
  60.             ROperateLine(image, RSubtractOperation, 0, h-2, w-3, 
  61.                    h-2, &cdelta);
  62.             RDrawLine(image, 0, h-1, w-1, h-1, &color);
  63.         } else {
  64.             ROperateLine(image, RSubtractOperation, 0, h-1, w-1, h-1,
  65.                    &cdelta);
  66.         }
  67.  
  68.     /* right */
  69.         if (bevel_type==RBEV_RAISED2 || bevel_type==RBEV_RAISED3) {
  70.             ROperateLine(image, RSubtractOperation, w-2, 0, w-2, h-2,
  71.                    &cdelta);
  72.             RDrawLine(image, w-1, 0, w-1, h-2, &color);
  73.         } else {
  74.             ROperateLine(image, RSubtractOperation, w-1, 0, w-1, h-2,
  75.                    &cdelta);
  76.         }
  77.     } else { /* sunken */
  78.         cdelta.alpha = 0;
  79.         cdelta.red = cdelta.green = cdelta.blue = 40;
  80.         ROperateLine(image, RSubtractOperation, 0, 0, w-1, 0, 
  81.                    &cdelta);        /* top */
  82.         ROperateLine(image, RSubtractOperation, 0, 1, 0, h-1, 
  83.                    &cdelta);    /* left */
  84.         cdelta.red = cdelta.green = cdelta.blue = 80;
  85.         ROperateLine(image, RAddOperation, 0, h-1, w-1, h-1, &cdelta); /* bottom */
  86.         ROperateLine(image, RAddOperation, w-1, 0, w-1, h-2, &cdelta); /* right */
  87.     }
  88. }
  89.  
  90.  
  91. void
  92. RFillImage(RImage *image, RColor *color)
  93. {
  94.     if (image->format == RRGBAFormat) {
  95.         unsigned char *d = image->data;
  96.         int i;
  97.  
  98.         for (i = 0; i < image->width; i++) {
  99.         *d++ = color->red;
  100.         *d++ = color->green;
  101.         *d++ = color->blue;
  102.         *d++ = color->alpha;
  103.         }
  104.         for (i = 1; i < image->height; i++, d += image->width*4) {
  105.         memcpy(d, image->data, image->width*4);
  106.         }
  107.     } else {
  108.         unsigned char *d = image->data;
  109.         int i;
  110.  
  111.         for (i = 0; i < image->width; i++) {
  112.         *d++ = color->red;
  113.         *d++ = color->green;
  114.         *d++ = color->blue;
  115.         }
  116.         for (i = 1; i < image->height; i++, d += image->width*3) {
  117.         memcpy(d, image->data, image->width*3);
  118.         }
  119.     }
  120. }
  121.  
  122.  
  123. void
  124. RClearImage(RImage *image, RColor *color)
  125. {
  126.     if (color->alpha==255) {
  127.     if (image->format == RRGBAFormat) {
  128.         unsigned char *d = image->data;
  129.         int i;
  130.  
  131.         for (i = 0; i < image->width; i++) {
  132.         *d++ = color->red;
  133.         *d++ = color->green;
  134.         *d++ = color->blue;
  135.         *d++ = 0xff;
  136.         }
  137.         for (i = 1; i < image->height; i++, d += image->width*4) {
  138.         memcpy(d, image->data, image->width*4);
  139.         }
  140.     } else {
  141.         unsigned char *d = image->data;
  142.         int i;
  143.         
  144.         for (i = 0; i < image->width; i++) {
  145.         *d++ = color->red;
  146.         *d++ = color->green;
  147.         *d++ = color->blue;
  148.         }
  149.         for (i = 1; i < image->height; i++, d += image->width*3) {
  150.         memcpy(d, image->data, image->width*3);
  151.         }
  152.     }
  153.     } else {
  154.     int bytes = image->width*image->height;
  155.     int i;
  156.     unsigned char *d;
  157.     int alpha, nalpha, r, g, b;
  158.  
  159.     d = image->data;
  160.  
  161.     alpha = color->alpha;
  162.     r = color->red * alpha;
  163.     g = color->green * alpha;
  164.     b = color->blue * alpha;
  165.     nalpha = 255 - alpha;
  166.  
  167.     for (i=0; i<bytes; i++) {
  168.         *d = (((int)*d * nalpha) + r)/256;
  169.         d++;
  170.         *d = (((int)*d * nalpha) + g)/256;
  171.         d++;
  172.         *d = (((int)*d * nalpha) + b)/256;
  173.         d++;
  174.         if (image->format == RRGBAFormat) {
  175.         d++;
  176.         }
  177.     }
  178.     }
  179. }
  180.  
  181. const char*
  182. RMessageForError(int errorCode)
  183. {
  184.     switch (errorCode) {
  185.      case RERR_NONE:
  186.     return "no error";
  187.  
  188.      case RERR_OPEN:
  189.     return "could not open file";
  190.  
  191.      case RERR_READ:
  192.     return "error reading from file";
  193.  
  194.      case RERR_WRITE:
  195.     return "error writing to file";
  196.  
  197.      case RERR_NOMEMORY:
  198.     return "out of memory";
  199.  
  200.      case RERR_NOCOLOR:
  201.     return "out of color cells";
  202.  
  203.      case RERR_BADIMAGEFILE:
  204.     return "invalid or corrupted image file";
  205.  
  206.      case RERR_BADFORMAT:
  207.     return "the image format in the file is not supported and can't be loaded";
  208.  
  209.      case RERR_BADINDEX:
  210.     return "image file does not contain requested image index";
  211.  
  212.      case RERR_BADVISUALID:
  213.     return "request for an invalid visual ID";
  214.  
  215.      case RERR_STDCMAPFAIL:
  216.     return "failed to create standard colormap";
  217.  
  218.      case RERR_XERROR:
  219.     return "internal X error";
  220.  
  221.      default:
  222.      case RERR_INTERNAL:
  223.     return "internal error";
  224.     }
  225. }
  226.